AppleDrop = function()
{
	Sprite.call(this);

	this.gameLayer ;
	this.startView ;
	this.topBar ;
	//添加到舞台的事件侦听
	this.addEventListener(Event.ADDED_TO_STAGE , this.addedHandler);
};
FLASH.extend( AppleDrop , Sprite );

/**
 * 添加到舞台上
 * @param e
 */
AppleDrop.prototype.addedHandler = function( e )
{
	var target = e.getTarget();
	target.removeEventListener(Event.ADDED_TO_STAGE , target.addedHandler);
	target.loadRes();
};

AppleDrop.prototype.init = function()
{
	this.gameLayer = new GameLayer();
	this.addChild(this.gameLayer);
	
	this.topBar = new TopBar();
	this.addChild(this.topBar );
	
	this.startView = new StartView();
	this.startView.setX(200);
	this.startView.setY(200);
	this.addChild( this.startView );

	
	var _self = this ;
	//对全局事件进行处理
	globalEventHandler = function(e)
	{
		switch(e.getType() )
		{
			case GlobalEvent.GAME_START:
				_self.removeChild( _self.startView , true );
				_self.gameLayer.reset();
				_self.gameLayer.startGame();
				break ;
			case GlobalEvent.GAME_OVER:
				_self.gameLayer.stopGame();
				break ;
		}
	};
	//侦听全局事件
	GlobalDispatcher.getInstance().addEventListener(GlobalEvent.GAME_START , globalEventHandler );
	GlobalDispatcher.getInstance().addEventListener(GlobalEvent.GAME_OVER , globalEventHandler );
};

/**
 * 加载游戏所需的资源
 */
AppleDrop.prototype.loadRes = function()
{
	var txt = new TextField();
	txt.align = TextFieldAlign.LEFT ;
	txt.font="bold 25px Arial";
	txt.color = "#ffffff"; //白色
	txt.text="SimpleText Test";
	txt.setX( this.getStage().getStageWidth()/2-40);
	txt.setY( this.getStage().getStageHeight()/2-10);
	this.addChild(txt);
	//===============
	//===============
	var _self = this ;
	function proHandler(e){
		txt.text = "loading  "+ ((e.getLoaded() / e.getTotal()*100) >>0)+"%" ;
	};
	function completeHandler(e){
		ResManager.getInstance().removeEventListener( ProgressEvent.PROGRESS , proHandler );
		ResManager.getInstance().removeEventListener( Event.COMPLETE , completeHandler );
		_self.removeAllChildren(true);
		_self.init();
		_self = null ;
	}
	ResManager.getInstance().addEventListener( ProgressEvent.PROGRESS , proHandler );
	ResManager.getInstance().addEventListener( Event.COMPLETE , completeHandler );
	ResManager.getInstance().startLoad();
};